package com.mt.mtyx.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mt.mtyx.entity.sys.RegionWare;
import com.mt.mtyx.sys.mapper.RegionWareMapper;
import com.mt.mtyx.sys.service.RegionWareService;
import com.mt.mtyx.vo.sys.RegionWareQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @author WMT
 * @Date 2023/7/18 11:02
 */
@Service
public class RegionWareServiceImpl extends ServiceImpl<RegionWareMapper, RegionWare> implements RegionWareService {

    @Override
    public IPage<RegionWare> selectAll(Page<RegionWare> warePage, RegionWareQueryVo queryVo) {
        // 1.获取条件查询 值
        String keyword = queryVo.getKeyword();

        // 2.封装查询条件
        LambdaQueryWrapper<RegionWare> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)) {
            // 根据区域名称 或 仓库名称 模糊查询
            wrapper.like(RegionWare::getRegionName, keyword)
                    .or()
                    .like(RegionWare::getWareName, keyword);
        }
        return baseMapper.selectPage(warePage, wrapper);
    }

    @Override
    public Integer countByRegionId(Long regionId) {
        LambdaQueryWrapper<RegionWare> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RegionWare::getRegionId, regionId);
        return Math.toIntExact(baseMapper.selectCount(wrapper));
    }

    @Override
    public int insert(RegionWare regionWare) {
        return baseMapper.insert(regionWare);
    }

    @Override
    public int delete(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        LambdaUpdateWrapper<RegionWare> wrapper = new LambdaUpdateWrapper<>();
        RegionWare regionWare = new RegionWare();
        regionWare.setStatus(status);
        wrapper.eq(RegionWare::getId, id);
        return baseMapper.update(regionWare, wrapper);
    }
}
